package com.carambatv.android.adapter;

import android.content.Context;
import android.database.Cursor;
import android.support.v4.widget.CursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.carambatv.android.R;
import com.carambatv.android.db.VideoQuery;
import com.carambatv.android.ui.CarambaApp;
import com.carambatv.android.utils.DateUtil;
import com.google.android.imageloader.ImageLoader;

public class VideoAdapter extends CursorAdapter {

	private LayoutInflater mInflater;
	private CarambaApp app;
	private final ImageLoader mImageLoader;

	public VideoAdapter(Context context, Cursor c) {
		super(context, c, false);
		mImageLoader = ImageLoader.get(context);
		mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		app = (CarambaApp) context.getApplicationContext();

	}

	@Override
	public void bindView(View view, final Context context, final Cursor cursor) {
		final YTubeViewHolder listItem = (YTubeViewHolder) view.getTag();
		final String title = getCursor().getString(VideoQuery.TITLE);
		final String type = getCursor().getString(VideoQuery.ID_VIDEO);
		final String thumbnail = getCursor().getString(VideoQuery.THUMBNAIL);
		final long date = getCursor().getLong(VideoQuery.DATE);

		mImageLoader.bind(this, listItem.image, thumbnail);
		listItem.title.setText(title);
		listItem.title.setTag(type);
		listItem.title.setTypeface(app.getTypefaceHelvetica());
		listItem.date.setText(context.getString(R.string.published).concat(" ").concat(DateUtil.parseToString(date)));
		listItem.date.setTypeface(app.getTypefaceHelvetica());

	}

	@Override
	public View newView(Context context, Cursor cursor, ViewGroup parent) {
		View view = mInflater.inflate(R.layout.list_item, null);
		final YTubeViewHolder listItem = new YTubeViewHolder();
		listItem.title = (TextView) view.findViewById(R.id.text_topic);
		listItem.image = (ImageView) view.findViewById(R.id.image);
		listItem.date = (TextView) view.findViewById(R.id.published);

		view.setTag(listItem);
		return view;

	}

	public class YTubeViewHolder {
		TextView title;
		ImageView image;
		TextView date;

	}

}
